#include "key.h"

volatile bool switchflag = 0; 
uint32_t last_press_time = 0;
const uint32_t debounce_time = 50;  

//sigle key
void Key(void) 
{
		
    static uint8_t key_state = 0; 
    static uint32_t press_timestamp = 0;
    switch (key_state) {
        case 0:  
            if (HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_10)== GPIO_PIN_SET) {
                press_timestamp = HAL_GetTick();
                key_state = 1;  
            }
            break;

        case 1:  
            if ((HAL_GetTick() - press_timestamp) > debounce_time) {
                if (HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_10) == GPIO_PIN_SET) {
                    switchflag = !switchflag;  //real press
                    key_state = 2;  
                } else {
                    key_state = 0; 
                }
            }
            break;

        case 2:  
            if (HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_10) == GPIO_PIN_RESET) {
                key_state = 3;  
            }
            break;

        case 3: 
            if ((HAL_GetTick() - press_timestamp) > debounce_time) {
                key_state = 0; 
            }
            break;
    }
			
		if(switchflag==1) //
		{		
			switch(SysPara.StateMachineFlag)
			{
						case Motor_StandBy:
							SysPara.StateMachineFlag=Wind;//EncoderMode;//Wind;//NonlinerFlux;//EncoderMode;//test;//NonlinerFlux;//Wind_time_test;//
					break;
					
					case EncoderMode:
							
					break;
					
					case NonlinerFlux:
							
					break;
					
					case Wind:
						
					break;
					
					case test:
				
					break;
					
					default:
						Motor_Init();
					break;			
							
				}
						
			
		}						
		else
		{		

	switch(SysPara.StateMachineFlag)
			{
					case Motor_StandBy:
						Motor_Init();
						PWMOutputs_DISABLE();						
					break;
					
					case EncoderMode:
							SysPara.StateMachineFlag=	Motor_StandBy;
							
					break;
					
					case NonlinerFlux:
							
						SysPara.StateMachineFlag=	Motor_StandBy;
			
					break;
					
					case Wind:
							SysPara.StateMachineFlag=	Motor_StandBy;
					break;
					
					case test:
							SysPara.StateMachineFlag=	Motor_StandBy;
					break;
					
					default:
						Motor_Init();
					break;			
							
				}			
		
			
		}
				
}
